import {Component} from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams, ViewController} from 'ionic-angular';
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";
import {Util} from "@providers/util";
import {WorkerProvider} from "@providers/interface/worker";

@IonicPage()
@Component({
  selector: 'page-worker-materiel-apply',
  templateUrl: 'worker-materiel-apply.html',
})
export class WorkerMaterielApplyPage {

  name: any;
  phone: number;
  address: any;
  safe: any;
  number: number;
  fixedCosts: any = [];//固定费用
  diyCosts: any = [];//自定义费用
  materialCosts: any = [];//辅材费用
  product_id = 0;
  SERVICETYPEID = 0;
  sum: number = 0;//总费用
  userInfo: any;
  QUANTITY: number;
  BJ: any;

  constructor(public navCtrl: NavController, public navParams: NavParams,
              private storage: StorageProvider, private dialog: DialogProvider,
              private modalCtrl: ModalController,
              private viewCtrl: ViewController,
              private worker: WorkerProvider) {
    this.userInfo = this.storage.read("userInfo");
  }

  ionViewDidLoad() {
    this.calculateSum();

    if (Util.isNull(this.navParams.data.ID)) {
      this.BJ = 0;
    } else {
      this.BJ = 1;
    }
    this.number = this.navParams.data.ID;
    this.name = this.navParams.data.NAME;
    this.phone = this.navParams.data.MOBILE;
    this.address = this.navParams.data.ADDRESS;
    console.info(this.navParams)
  }


  dismiss(data?: any) {
    if (data) {
      this.viewCtrl.dismiss(data);
      return;
    }
    this.viewCtrl.dismiss();
  }

  /**
   * 辅材数量改变点击事件
   * @param pro 选中的商品
   * @param value 为0时表示减小，为1时表示增加
   */

  quantityChange(cost: any, index?: number) {
    switch (index) {
      case 0:
        if (cost.QUANTITY == 0) return
        cost.QUANTITY = +(cost.QUANTITY - 1).toFixed(2);
        if (cost.QUANTITY < 0) {
          cost.QUANTITY = 0;
        }
        break;
      case 1:
        cost.QUANTITY = +(+cost.QUANTITY + 1).toFixed(2);
        break;
    }
    this.calculateSum();
  }

  /**
   * 确定按钮点击事件
   */
  onSubmit() {
    //guarantee保险类型
    console.info(this.userInfo);
    this.fixedCosts = this.fixedCosts.filter(value => {
      return value.QUANTITY > 0;
    });
    this.diyCosts = this.diyCosts.filter(value => {
      return value.QUANTITY > 0;
    });
    this.materialCosts = this.materialCosts.filter(value => {
      return value.QUANTITY > 0;
    });
    this.worker.insertParts({
      "NAME": this.name,
      "PHONE": this.phone,
      "ADDRESS": this.address,
      "BXLX": this.safe,
      "GDID": this.number,
      "fixedCosts": this.fixedCosts, "diyCosts": this.diyCosts, "materialCosts": this.materialCosts,
      "SUM": this.sum,
      "partyId": this.userInfo.PARTYID
    }).then(message => {
      this.dismiss();
      console.log(message, "fixedCosts", this.fixedCosts, "diyCosts", this.diyCosts, "materialCosts", this.materialCosts)
    }).catch(e => {
      console.log(e)
    })
  }

  /**
   * 计算总费用
   */
  calculateSum() {
    this.sum = 0;
    this.fixedCosts.forEach(value => {
      this.sum = +(this.sum + +(value.PRICE * value.QUANTITY).toFixed(2)).toFixed(2);
    });
    this.diyCosts.forEach(value => {
      this.sum = +(this.sum + +(value.PRICE * value.QUANTITY).toFixed(2)).toFixed(2);
    });
    this.materialCosts.forEach(value => {
      this.sum = +(this.sum + +(value.PRICE * value.QUANTITY).toFixed(2)).toFixed(2);
    });
  }

  //失去焦点改变辅材数量
  eqc(cost: any) {
    this.editChange(cost);
  }

  //点击enter改变辅材数量
  kdqc(event, cost: any) {
    if (event.keyCode == 13) {
      this.editChange(cost);
    }
  }

  editChange(cost: any) {
    this.materialCosts.forEach(value => {
      if (value.ZJ == cost.ZJ) {
        if (cost.QUANTITY == null) {
          value.QUANTITY = 0;
          cost.QUANTITY = 0;
        } else {
          cost.QUANTITY = +cost.QUANTITY.toFixed(2);
          if (cost.QUANTITY < 0) {
            cost.QUANTITY = 0;
          }
          value.QUANTITY = cost.QUANTITY;
        }
      }
    });
    this.fixedCosts.forEach(value => {
      if (value.ZJ == cost.ZJ) {
        if (cost.QUANTITY == null) {
          value.QUANTITY = 0;
        } else {
          if (cost.QUANTITY < 0) {
            cost.QUANTITY = 0;
          }
          value.QUANTITY = cost.QUANTITY;
        }
      }
    });
    this.calculateSum();
  }

  /**
   * 创建modal
   * @param page
   * @param data
   */
  showMaterialModal() {
    let modal = this.modalCtrl.create('WorkerMaterielStorePage',
      {
        "materialCosts": this.materialCosts,
        "product_id": this.product_id,
        "product_price": this.QUANTITY,
        "product_sum": this.sum,
        "SERVICETYPEID": this.SERVICETYPEID,
        "partyid": this.userInfo.PARTYID
      });
    modal.onDidDismiss(data => {
      if (data) {
        let isError = false;
        this.fixedCosts.forEach(value => {
          data.forEach(val => {
            if (value.PRODUCT_ID == val.PRODUCT_ID) {
              isError = true;
            }
          });
        });
        if (isError) {
          this.dialog.showAlert("该商品已有的辅材不允许额外多选！");
          return;
        }
        this.materialCosts = data;
        this.calculateSum();
      }
    });
    modal.present();
  }

}
